package com.example.visitedsys.Controller;

import com.example.visitedsys.Shrio.Bean.ResponseBean;
import com.example.visitedsys.informationGet.PermissionNodeVO;
import com.example.visitedsys.informationGet.PermissionVO;
import com.example.visitedsys.service.PermissionService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author  SongPeng
 * @date  2021/10/19 14:21
 * @version 1.0
 */

@RestController
@RequestMapping("/permission")

public class PermissionController {
    @ModelAttribute
    public void doString(){
        MDC.put("operateModuleName","权限管理模块");
    }

    @Autowired
    private PermissionService permissionService;

    /**
     * 加载菜单树
     *
     * @return
     */
    @ApiOperation(value = "加载菜单树", notes = "获取所有菜单树，以及展开项")
    @GetMapping("/tree")
    public ResponseBean tree() {
        List<PermissionNodeVO> permissionTree = permissionService.findMenuTree();
        List<Long> ids = permissionService.findOpenIds();
        Map<String, Object> map = new HashMap<>();
        map.put("tree", permissionTree);
        map.put("open", ids);
        return ResponseBean.success(map);
    }

//    /**
//     * 新增菜单/按钮
//     *
//     * @return
//     */
//    @ControllerEndpoint(exceptionMessage = "新增菜单/按钮失败", operation = "新增菜单/按钮")
//    @ApiOperation(value = "新增菜单")
//    @RequiresPermissions({"menu:add"})
//    @PostMapping("/add")
//    public ResponseBean add(@RequestBody @Validated MenuVO menuVO) {
//        Menu node = menuService.add(menuVO);
//        Map<String, Object> map = new HashMap<>();
//        map.put("id", node.getId());
//        map.put("menuName", node.getMenuName());
//        map.put("children", new ArrayList<>());
//        map.put("icon", node.getIcon());
//        return ResponseBean.success(map);
//    }

//    /**
//     * 删除菜单/按钮
//     *
//     * @param id
//     * @return
//     */
//    @ControllerEndpoint(exceptionMessage = "删除菜单/按钮失败", operation = "删除菜单/按钮")
//    @ApiOperation(value = "删除菜单", notes = "根据id删除菜单节点")
//    @RequiresPermissions({"menu:delete"})
//    @DeleteMapping("/delete/{id}")
//    public ResponseBean delete(@PathVariable Long id) {
//        menuService.delete(id);
//        return ResponseBean.success();
//    }

    /**
     * 菜单详情
     *
     * @param permissionId
     * @return
     */
    @RequiresPermissions({"menu:edit"})
    @GetMapping("/edit/{permisionId}")
    public ResponseBean edit(@PathVariable Long permissionId) {
        PermissionVO permissionVO = permissionService.edit(permissionId);
        return ResponseBean.success(permissionVO);
    }

//    /**
//     * 更新菜单
//     *
//     * @param id
//     * @param menuVO
//     * @return
//     */
//    @ControllerEndpoint(exceptionMessage = "更新菜单失败", operation = "更新菜单")
//    @ApiOperation(value = "更新菜单", notes = "根据id更新菜单节点")
//    @RequiresPermissions({"menu:update"})
//    @PutMapping("/update/{id}")
//    public ResponseBean update(@PathVariable Long id, @RequestBody @Validated MenuVO menuVO) {
//        menuService.update(id, menuVO);
//        return ResponseBean.success();
//    }

//    /**
//     * 导出excel
//     * @param response
//     */
//    @ApiOperation(value = "导出excel", notes = "导出所有菜单的excel表格")
//    @PostMapping("excel")
//    @RequiresPermissions("menu:export")
//    @ControllerEndpoint(exceptionMessage = "导出Excel失败",operation = "导出菜单excel")
//    public void export(HttpServletResponse response) {
//        List<Menu> menus = this.menuService.findAll();
//        ExcelKit.$Export(Menu.class, response).downXlsx(menus, false);
//    }

}
